//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.tyy;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.security.Security;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AESUtil {
    public AESUtil() {
    }

    public static String encryptTo(String content, String aeskey) throws Exception {
        if (content != null && !content.equals("")) {
            try {
                byte[] bytePassword = aeskey.getBytes("UTF-8");
                Cipher cipherInstance = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
                SecretKeySpec key = new SecretKeySpec(bytePassword, "AES");
                cipherInstance.init(1, key);
                byte[] byteContent = content.getBytes("UTF-8");
                byte[] cryptograph = cipherInstance.doFinal(byteContent);
                String cryptographStr = Base64.encode(cryptograph);
                return cryptographStr;
            } catch (Exception var8) {
                throw new Exception("AES加密失败" + var8);
            }
        } else {
            return null;
        }
    }

    public static String decryptTo(String content, String aeskey) throws Exception {
        byte[] bytePassword = null;
        byte[] bContent = null;

        try {
            bytePassword = aeskey.getBytes("UTF-8");
            if (content == null || content.length() == 0) {
                return null;
            }

            bContent = Base64.decode(content);
        } catch (Exception var8) {
        }

        try {
            Cipher cipherInstance = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            SecretKeySpec key = new SecretKeySpec(bytePassword, "AES");
            cipherInstance.init(2, key);
            byte[] crypted = cipherInstance.doFinal(bContent);
            return new String(crypted, "utf-8");
        } catch (Exception var7) {
            throw new Exception("AES加密失败" + var7);
            // return content;
        }
    }

    public static void main(String[] args) throws Exception {
        String testData = "testData";
        String aesKey = "91qdwE8vAQ1BcfYCpESUsnVzOOMA2ZSd";

        int num;
        do {
            Scanner input = new Scanner(System.in);
            System.out.println("加密请输入1，解密请输入2，默认加密：");
            num = input.nextInt();
            String decryptData;
            if (num == 2) {
                System.out.println("请输入待解密内容：");
                testData = input.next();
                decryptData = decryptTo(testData, aesKey);
                System.out.println("解密后的结果：" + decryptData);
            } else {
                System.out.println("请输入待加密内容：");
                testData = input.next();
                decryptData = encryptTo(testData, aesKey);
                System.out.println("加密后的结果：" + decryptData);
            }

            System.out.println("退出请输入1，继续请输入2，默认退出：");
            num = input.nextInt();
        } while(num == 2);

    }

    static {
        try {
            Security.addProvider(new BouncyCastleProvider());
        } catch (Exception var1) {
            var1.printStackTrace();
        }

    }
}
